//
//  UIScrollView+BCRefreshExt.m
//  BCScrollViewKit
//
//  Created by chasel. c on 2022/8/25.
//

#import "UIScrollView+BCRefreshExt.h"

@implementation UIScrollView (BCRefreshExt)

///MARK: -Header
- (void)bc_addHeader:(MJRefreshComponentAction)refreshingBlock {
    self.mj_header = [self.class bc_configHeaderWithBlock:refreshingBlock];
}

- (UIScrollView * (^)(NSObject *target))addHeaderTarget {
    return ^UIScrollView *(NSObject *target) {
               __weak __typeof(target) weakTarget = target;

               self.mj_header = [UIScrollView bc_configHeaderWithBlock:^{
            SEL action = NSSelectorFromString(@"fetchDataWithLoadingMore:");

            if ([weakTarget respondsToSelector:action]) {
                [weakTarget performSelectorOnMainThread:action
                                             withObject:@(NO)
                                          waitUntilDone:YES];
            } else {
#ifdef DEBUG
                NSAssert(false, @"请在 %@ 中实现 [fetchDataWithLoadingMore:] 方法", weakTarget);
#endif
            }
        }];
               return self;
    };
}

- (UIScrollView * (^)(MJRefreshComponentAction block))addHeader {
    return ^UIScrollView *(MJRefreshComponentAction block) {
               self.mj_header = [self.class bc_configHeaderWithBlock:block];
               return self;
    };
}

///MARK: -Footer
- (void)bc_addFooter:(MJRefreshComponentAction)refreshingBlock {
    self.mj_footer = [self.class bc_configFooterWithBlock:refreshingBlock];
}

- (UIScrollView * (^)(NSObject *target))addFooterTarget {
    return ^UIScrollView *(NSObject *target) {
               __weak __typeof(target) weakTarget = target;
               self.mj_footer = [UIScrollView bc_configFooterWithBlock:^{
            SEL action = NSSelectorFromString(@"fetchDataWithLoadingMore:");

            if ([weakTarget respondsToSelector:action]) {
                [weakTarget performSelectorOnMainThread:action
                                             withObject:@(YES)
                                          waitUntilDone:YES];
            } else {
#ifdef DEBUG
                NSAssert(false, @"请在 %@ 中实现 [fetchDataWithLoadingMore:] 方法", weakTarget);
#endif
            }
        }];
               return self;
    };
}

- (UIScrollView * (^)(MJRefreshComponentAction block))addFooter {
    return ^UIScrollView *(MJRefreshComponentAction block) {
               self.mj_footer = [self.class bc_configFooterWithBlock:block];
               return self;
    };
}

+ (MJRefreshNormalHeader *)bc_configHeaderWithBlock:(MJRefreshComponentAction)block {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:block];

    header.lastUpdatedTimeLabel.hidden = YES;
    return header;
}

+ (MJRefreshBackNormalFooter *)bc_configFooterWithBlock:(MJRefreshComponentAction)block {
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:block];

    return footer;
}

///MARK: -end
- (BOOL)bc_isHeaderRefreshing {
    return self.mj_header.refreshing;
}
- (void)bc_headerEndRefreshing {
    [self.mj_header endRefreshing];
}
- (void)bc_hidenHeader {
    self.mj_header.hidden = YES;
}
- (void)bc_removeHeader {
    [self.mj_header removeFromSuperview];
    [self layoutIfNeeded];
}
- (BOOL)bc_isFooterRefreshing {
    return self.mj_footer.refreshing;
}
- (void)bc_footerEndRefreshing {
    [self.mj_footer endRefreshing];
}
- (void)bc_endRefreshingWithNoMoreData {
    [self.mj_footer endRefreshingWithNoMoreData];
}

///MARK: -Helper
- (UIScrollView * (^)(Class aclass))registerCellClass {
    return ^UIScrollView *(Class aclass) {
               [self bc_registerCellClass:aclass];

               return self;
    };
}

- (void)bc_registerCellClass:(Class)aclass {
    if ([self isKindOfClass:UITableView.class]) {
        UITableView *tableView = (UITableView *)self;
        [tableView registerClass:aclass forCellReuseIdentifier:NSStringFromClass(aclass)];
    } else if ([self isKindOfClass:UICollectionView.class]) {
        UICollectionView *collectionView = (UICollectionView *)self;
        [collectionView registerClass:aclass forCellWithReuseIdentifier:NSStringFromClass(aclass)];
    } else {
        NSLog(@"self is not UITableView or UICollectionView");
    }
}

- (UIScrollView * (^)(Class aclass))registerHeaderClass {
    return ^UIScrollView *(Class aclass) {
               [self bc_registerHeaderClass:aclass];

               return self;
    };
}

- (void)bc_registerHeaderClass:(Class)aclass {
    if ([self isKindOfClass:UITableView.class]) {
        UITableView *tableView = (UITableView *)self;
        [tableView registerClass:aclass forHeaderFooterViewReuseIdentifier:NSStringFromClass(aclass)];
    } else if ([self isKindOfClass:UICollectionView.class]) {
        UICollectionView *collectionView = (UICollectionView *)self;
        [collectionView registerClass:aclass forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass(aclass)];
    } else {
        NSLog(@"self is not UITableView or UICollectionView");
    }
}

@end
